package com.chwn.harulib.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**

 */
public class ViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> mViews;
    private View              mConvertView;

    /**
     * 私有构造方法
     * @param itemView
     */
    private ViewHolder(View itemView) {
        super(itemView);
        mConvertView = itemView;
        mViews = new SparseArray<>();
    }

    public static ViewHolder create(Context context, int layoutId, ViewGroup parent) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        return new ViewHolder(itemView);
    }

    public static ViewHolder create(View itemView) {
        return new ViewHolder(itemView);
    }

    /**
     * 通过id获得控件
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return mConvertView;
    }

    public View getSwipeView() {
        ViewGroup itemLayout = ((ViewGroup) mConvertView);
        if (itemLayout.getChildCount() == 2) {
            return itemLayout.getChildAt(1);
        }
        return null;
    }

    public void setText(int viewId, String text) {
        TextView textView = getView(viewId);
        textView.setText(text);
    }

    public void setTextInt(int viewId, int content) {
        TextView textView = getView(viewId);

        textView.setText(String.valueOf(content));
    }


    public void setOnClickListener(int viewId, View.OnClickListener clickListener) {
        View view = getView(viewId);
        view.setOnClickListener(clickListener);
    }

    public void setBgRes(int viewId, int resId) {
        View view = getView(viewId);
        view.setBackgroundResource(resId);
    }

    public void setBgColor(int viewId, int colorId) {
        View view = getView(viewId);
        view.setBackgroundColor(colorId);
    }

    public void setTextWorkTime(int viewId, int duration) {
        TextView mTvWrokTime = getView(viewId);

        /**
         * 126/60
         */
        int minute = duration / 60;
        int seconds = duration - minute * 60;
        mTvWrokTime.setText(minute + "'" + seconds + "\"");
    }

    /**
     * Fresco加载res 资源
     * @param resId
     * @param
     * @param
     */
    public void setResImage(int resId, int drawableResId) {
        ImageView view = getView(resId);
        view.setImageResource(drawableResId);
    }

    public void setViewVisibility(int resId) {
        View view = getView(resId);
        view.setVisibility(View.VISIBLE);
    }

    public void setViewGone(int resId) {
        View view = getView(resId);
        view.setVisibility(View.GONE);
    }

    public void setMarginBottom(int resId) {
        View view = getView(resId);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        layoutParams.bottomMargin = 26;
        view.setLayoutParams(layoutParams);
    }
}
